package com.ypf.controller;

import com.ypf.domain.MessageVo;
import com.ypf.service.Impl.SseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import javax.websocket.server.PathParam;

@RestController
@RequestMapping("/sse")
public class SseController {
    @Autowired
    private SseServiceImpl sseService;

    @CrossOrigin
    @GetMapping("/createConnect")
    public SseEmitter createConnect(String clientId){
       return sseService.createConnect(clientId);
    }

    @CrossOrigin
    @PostMapping("/broadcast")
    public void sendMessageToAllClient(@RequestBody(required = false) String msg){
        sseService.sendMessageToAllClient(msg);
    }

    @CrossOrigin
    @PostMapping("/sendMessage")
    public void sendMessageToOneClient(@RequestBody(required = false) MessageVo messageVo){
        if (messageVo.getClientId().isEmpty()){
            return;
        }
        sseService.sendMessageToOneClient(messageVo.getClientId(),messageVo.getMsg());
    }

    @CrossOrigin
    @GetMapping("/closeConnect")
    public void closeConnect(@RequestParam(required = true) String clientId){
        sseService.closeConnect(clientId);
    }

}
